<template></template>
<script>
  import {getWxCode, getWxInfoByCode} from '../../../utils/wx_utils'
  import {getCookie, getParameter, setCookie} from 'as-common'
  import store from '../../../repo/store/store'
  import {authActionsTypes} from '../../../repo/store/auth'
  import UserAPI from '../../../repo/api/UserAPI'
  import {userActionsTypes, userTypes} from '../../../repo/store/user'
  import {push} from "../../../router/util";
  import {LoginType} from "../../../repo/env/Constants";

  export default {
    name: 'wx-login',
    created() {
      let code = getParameter('code');
      let loginPath = getCookie(LoginType.jumpAfterLogin, true);

      // If navigation bar address has this information code
      if (code) {
        getWxInfoByCode(code, (res) => {
          store.dispatch(authActionsTypes.LOGIN_BY_WE_CHAT, {wxInfo: res}).then((result) => {
            UserAPI.getUserInfo().then((result) => {
              this.$store.commit(userTypes.SAVE_USER_INFO, result.responseData);
              this.$store.dispatch(userActionsTypes.FETCH_ENTERPRISE_INFO).then(() => {
                //这里不管有没有返回企业信息，都可以进入。
                if (loginPath) {
                  this.$_router.push('/transfer-page');
                  setCookie('jumpKey', true);
                } else {
                  push('')
                }
              }).catch((e) => {
                console.log('登录出错', loginPath, e);
                this.$router.push({path: loginPath.fullPath});
              })
            })
          }).catch(e => {
            console.error(e)
          })

        })
      } else {
        getWxCode()
      }
    }
  }
</script>
